#include "muinformation.h"
#include "ui_muinformation.h"
#include "iostream"
#include "QStandardItem"
#include "QTableWidgetItem"
#include "addlayout.h"
#include "cinemasystem.h"

using namespace std;

extern std::string username;

struct UserEntry {
    std::string username;
    std::string password;
    int hallnumber;
    std::string seatid;

};

struct Entry {
    string username;
    string password;
    int hallnumber;
    string seatid;
};

std::vector<UserEntry> load(const std::string& filename) {
    std::ifstream infile(filename);

    if (!infile.is_open()) {
        std::cerr << "Failed to open the file for reading." << std::endl;
        return {};
    }

    std::vector<UserEntry> entries;

    std::string line;
    while (std::getline(infile, line)) {
        std::istringstream iss(line);
        UserEntry entry;

        if (!(iss >> entry.username >> entry.password >> entry.hallnumber >>entry.seatid)) {
            std::cerr << "Failed to parse line: " << line << std::endl;
            continue;  // skip this line and move to the next one
        }

        entries.push_back(entry);
    }

    return entries;
}

MuInformation::MuInformation(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MuInformation)
{
    ui->setupUi(this);
    QStandardItemModel *model = new QStandardItemModel();
    auto entries = load("D:\\QT_projects\\Login\\3.txt");

    CinemaSystem cinemasystem("2023-10-17", "Sample Movie", 100.0);
    cinemasystem.halls= cinemasystem.readAuditoriumsFromFile("D:\\QT_projects\\Login\\2.txt");

    string targetUsername = username;  // 要提取信息的目标用户名

    int i=0;

    vector<Entry> matchingEntries;
    for (const auto& entry : entries) {
        if (entry.username == targetUsername) {
            Entry matchingEntry;
            matchingEntry.hallnumber = entry.hallnumber;
            matchingEntry.seatid = entry.seatid;
            matchingEntries.push_back(matchingEntry);
            i++;
        }
    }

    model->setRowCount(i);
    model->setColumnCount(4);

    QStringList headers;
    headers << "用户名" << "放映厅" << "座位"<< "时间";
    model->setHorizontalHeaderLabels(headers);

    for (int row = 0; row < i; ++row) {
        for (int col = 0; col <= 3; ++col) {
            QModelIndex index = model->index(row, col);
            int hal_num;
            if(col == 0){
                QString qusername = QString::fromStdString(username);//电影时间
                model->setData(index,qusername);
            }else if(col == 1){
                hal_num = matchingEntries[row].hallnumber;
                QString str = QString::number(hal_num);
                model->setData(index,str);
            }else if(col == 2){
                QString qseat = QString::fromStdString(matchingEntries[row].seatid);
                model->setData(index,qseat);
            }else if(col == 3){
                std::string __filmtime = cinemasystem.halls[hal_num].movie.begin()->first;
                QString filmtime = QString::fromStdString(__filmtime);
                model->setData(index,filmtime);
            }
        }
    }

    ui->tableView->horizontalHeader()->resizeSection(0, 300);
    //ui->tableView->columnWidth(0);
    ui->tableView->setModel(model);


}

MuInformation::~MuInformation()
{
    delete ui;
}
